package com.springboot_gradrecruit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.common.R;
import com.springboot_gradrecruit.model.dto.bulletin.AddBulletinRequest;
import com.springboot_gradrecruit.model.dto.bulletin.PageBulletinRequest;
import com.springboot_gradrecruit.model.dto.bulletin.UpdateBulletinRequest;
import com.springboot_gradrecruit.model.entity.Bulletin;
import com.springboot_gradrecruit.model.vo.bulletin.BulletinVO;
import com.springboot_gradrecruit.service.BulletinService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zr
 */
@Tag(name = "公告相关接口")
@RestController
@RequestMapping("/bulletin")
@Slf4j
public class BulletinController {

    @Resource
    private BulletinService bulletinService;

    //1-1.新增公告
    @Operation(summary = "新增公告")
    @PostMapping("/addBulletin")
    public R<Boolean> addBulletin(@RequestBody AddBulletinRequest addBulletinRequest) {
        //1. 校验参数是否为空
        if (addBulletinRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Long aLong = bulletinService.addBulletin(addBulletinRequest);
        if (aLong > 0) {
            return R.success(true, "添加成功");
        }
        return R.error(false, "添加失败");

    }

    //2-1.分页获取公告
    @Operation(summary = "分页获取公告")
    @PostMapping("/pageBulletin")
    public R<PageResponse<BulletinVO>> pageBulletin(@RequestBody PageBulletinRequest pageBulletinRequest) {
        //1.调用service
        PageResponse<BulletinVO> pageResponse = bulletinService.pageBulletin(pageBulletinRequest);
        return R.success(pageResponse, "查询成功");
    }

    //2-2.获取公告详情根据id
    @Operation(summary = "获取公告详情根据id")
    @PostMapping("/queryBulletinDetailById")
    public R<BulletinVO> queryBulletinDetailById(Long bulletinId) {
        //1.参数校验
        if (bulletinId == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        Bulletin bulletin = bulletinService.getById(bulletinId);
        BulletinVO bulletinVO = BeanUtil.copyProperties(bulletin, BulletinVO.class);
        return R.success(bulletinVO, "查询成功");
    }

    //3-1.删除公告
    @Operation(summary = "删除公告")
    @PostMapping("/deleteBulletin")
    public R<Boolean> deleteBulletin(Long bulletinId) {
        //1.调用service
        boolean b = bulletinService.removeById(bulletinId);
        if (b) {
            return R.success(true, "删除成功");
        }
        return R.error(false, "删除失败");
    }

    //4-1.更新公告
    @Operation(summary = "更新公告")
    @PostMapping("/updateBulletin")
    public R<Boolean> updateBulletin(@RequestBody UpdateBulletinRequest updateBulletinRequest) {
        //1.参数校验
        if (updateBulletinRequest == null) {
            throw new RuntimeException("参数为空");
        }
        //2.调用service
        boolean updatedBulletin = bulletinService.updateBulletinById(updateBulletinRequest);
        if (updatedBulletin) {
            return R.success(true, "修改成功");
        }
        return R.error(false, "修改失败");
    }
}
